package com.solotiger.douban.ui.fragment.movie;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.solotiger.douban.R;
import com.solotiger.douban.biz.BaseSubscriber;
import com.solotiger.douban.biz.api.MovieApi;
import com.solotiger.douban.biz.model.movie.SubjectResult;
import com.solotiger.douban.ui.adapter.MovieAdapter;
import com.solotiger.douban.ui.base.BaseBarFragment;
import com.solotiger.douban.ui.view.divider.Divider;
import com.solotiger.douban.util.DisplayUtil;
import com.solotiger.douban.util.RxHelper;
import com.solotiger.douban.util.ScreenUtil;

import butterknife.BindView;

/**
 * 热映
 */
public class TheaterFragment extends BaseBarFragment {

    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private GridLayoutManager mLayoutManager;

    private int mColumnCount = 3;

    private MovieAdapter mAdapter;

    /**
     * count 一页最大数据
     */
    private int start = 0, count = 15;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init();
    }

    /**
     * 初始化
     */
    private void init() {

        initRecyclerView();

        ProgressLayout dotView = new ProgressLayout(getContext());
        dotView.setColorSchemeResources(R.color.colorPrimary);
        mRefreshLayout.setHeaderView(dotView);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                loadData();
            }
        });

        mRefreshLayout.setEnableLoadmore(false);

        mRefreshLayout.startRefresh();
    }

    private void initRecyclerView() {
        mLayoutManager = new GridLayoutManager(getContext(), mColumnCount);
        mRecyclerView.setLayoutManager(mLayoutManager);
        Divider divider = DisplayUtil.getDivider(Color.WHITE);
        mRecyclerView.addItemDecoration(divider);
        int itemSize = (ScreenUtil.getCurrentScreenWidth(getContext()) - divider.getWidth() * (mColumnCount + 1)) / mColumnCount;

        mAdapter = new MovieAdapter(getContext(), itemSize, true);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Fresco.getImagePipeline().resume();
                } else {
                    Fresco.getImagePipeline().pause();
                }
            }
        });

        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mAdapter.clear();
//        loading();
        MovieApi.getMovieInTheaters(60)
                .compose(RxHelper.bind(this))
                .subscribe(new BaseSubscriber<SubjectResult>() {
                    @Override
                    protected void _onError(Throwable e) {
//                        closeLoading();
                        mRefreshLayout.finishRefreshing();
                        showMessage("失败!");
                    }

                    @Override
                    public void onNext(SubjectResult subjectResult) {
//                        closeLoading();
                        mRefreshLayout.finishRefreshing();
                        if (subjectResult.getSubjects() != null) {
                            mAdapter.notifyDataSetChanged(subjectResult.getSubjects());
                        }
                    }
                });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_theater;
    }


}
